<template>
	<view>
		<u-mask :show="loading" :custom-style="{background: 'rgba(0, 0, 0, 0)'}">
			<view class="warp">
				<u-loading mode="flower"></u-loading>
			</view>
		</u-mask>
		<view class="back" v-if="!is_play">
			<uni-icons type="back" color="#dbdbdb" @click="to_detail" size="30"></uni-icons>
		</view>
		<view v-if="class_list.length > 0">
			<view class="video-img" v-if="!is_play" :style="{'background-image': 'url(' + video_image + ')'}" @click="is_play = 1">
				<u-icon style="margin-top: 160rpx;" name="../../static/play.png" size="100"></u-icon>
			</view>
			<video v-else :src="video_link" :direction="direction" style="width: 100%;" :controls="true" :autoplay="true">
				<cover-image class="back" style="top: 35rpx;left:35rpx;" src="../../static/back.png" @click="to_detail"></cover-image>
			</video>
			<swiper :style="{'height':swiperHeight+'px'}" @change="swiperChange" @animationfinish="swiperAnimationFinish">
				<swiper-item v-for="(item,key) in class_list">
					<view style="padding: 30rpx;" :id="'swiper-item' + key">
						<view style="color: #999999;font-size: 30rpx;width: 100%;">
							<rich-text style="width: 50%;float: left;" :nodes="'第' + (key + 1) + '题'"></rich-text>
							<rich-text style="width: 50%;float: left;text-align: right;" :nodes=" (key + 1) + '/' + class_list.length"></rich-text>
						</view>
						<view style="margin-top: 60rpx;">
							<view style="font-size: 35rpx;">
								<text style="color: #00B26A;">
									[{{type_list[item.class_type]}}]
								</text>
								<text>{{item.class_name}}:</text>
							</view>
							<view class="content">
								<view v-if="item.class_type == 0 || item.class_type == 1">
									<u-cell-group :border="false">
										<u-cell-item v-for="(answer,answer_key) in item.answer_list" 
										:border-bottom="false" :arrow="false" @click="tip(key,answer_key)"
										:style="'background-color:' + (answer.is_tip?'#DCDCDC':'') + ';color:' + (answer.is_tip?(answer.is_success?'#00B26A':'#FA3534'):(item.is_tip?(answer.is_success?'#00B26A':''):''))">
											<template slot="icon">
												<view style="width: 60rpx;border: 2rpx solid #999999;text-align: center;border-radius: 50%;">
													{{answer_key_list[answer_key]}}
												</view>
											</template>
											<template slot="title">
												<view style="word-break:break-all;margin-left: 30rpx;">{{answer.answer}}</view>
											</template>
										</u-cell-item>
									</u-cell-group>
								</view>
								<view v-if="item.class_type == 2 || item.class_type == 4">
									<view style="margin-top: 30rpx;">
										<u-input type="textarea" :autoHeight="true" style="height: 350rpx;"/>
										<view style="text-align: right;">
											<u-button type="success" size="medium" @click="tip_orther(key)">确认</u-button>
										</view>
									</view>
								</view>
								<view v-if="item.class_type == 3">
									<view style="margin-top: 30rpx;">
										<u-button type="success" size="medium" @click="tip_orther(key)">正确</u-button>
										<u-button type="error" size="medium" @click="tip_orther(key)">错误</u-button>
									</view>
								</view>
							</view>
						</view>
						<view v-if="item.is_tip == 1">
							<view v-if="item.class_type != 4">
								<view style="margin-top: 30rpx;">
									正确答案：
									<text v-if="item.class_type != 3" style="color: #00B26A;">{{item.correct_answer}}</text>
									<text v-else style="color: #00B26A;">
										{{item.correct_answer?'正确':'错误'}}
									</text>
								</view>
							</view>
							<view class="content">
								<view style="padding: 20rpx;">
									<u-icon name="star" color="#00B26A"></u-icon>
									<text style="color: #000000;font-weight: 900;margin-left: 20rpx;">解析详情</text>
								</view>
								<view style="padding: 20rpx;">
									{{item.answer_analysis}}
								</view>
							</view>
						</view>
					</view>
				</swiper-item>
			</swiper>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'; 
	export default {
		computed: mapState(['uerInfo']),
		data() {
			return {
				loading: true,
				product_id: 0,
				class_list:[],
				answer_key_list:['A','B','C','D','E','F','G','H','I','J'],
				type_list:['单选','多选','简答','判断','写作'],
				video_link:'',
				video_image:'',
				is_play:0,
				swiperHeight:400,
				current:0,
				direction:-90
			}
		},
		onLoad(option) {
			this.product_id = option["pruduct_id"];
			this.get_class_list();
		},
		methods: {
			get_class_list() {
				uni.request({
					url: `${this.$serverUrl}/class_list`,
					header: {
						"Content-Type": "application/json"
					},
					data: {
						"product_id": this.product_id,
						"uid": this.uerInfo.uid,
						"token": this.uerInfo.token,
					},
					method: "GET",
					success: (e) => {
						this.loading = false;
						if (e.data.status == 200) {
							this.class_list = e.data.data;
							if(this.class_list.length > 0){
								this.video_link = this.class_list[0]['video_link'];
								this.video_image = this.class_list[0]['video_image'];
							}
						} else {
							this.$toast_fail(e.data);
						}
					},
					fail: (e) => {
						this.$toast_fail({msg:"请求失败"});
					}
				});
			},
			to_detail() {
				uni.navigateBack({
					animationType:'pop-out'
				})
			},
			tip(key,answer_key) {
				if(this.class_list[key]['is_tip'] != 1){
					this.class_list[key]['answer_list'][answer_key]['is_tip'] = 1;
					var tip_count = 0;
					this.class_list[key]['answer_list'].find(function(v){
						if(v.is_tip == 1) {
							tip_count++
						}
					});
					if(tip_count == this.class_list[key]['correct_answer_count']){
						this.class_list[key]['is_tip'] = 1;
					}
				}
			},
			tip_orther(key){
				this.class_list[key]['is_tip'] = 1;
			},
			swiperChange(e) {
				this.is_play = 0;
				this.video_link = this.class_list[e.target.current]['video_link'];
				this.video_image = this.class_list[e.target.current]['video_image'];
			},
			swiperAnimationFinish(e) {
				this.setSwiperHeight(e.target.current);
			},
			setSwiperHeight(index) {
				const query = uni.createSelectorQuery().in(this);
				query.select('#swiper-item' + index).boundingClientRect(data => {
					this.swiperHeight = data.height;
				}).exec();
			}
		}
	}
</script>

<style>
	.warp {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 100%;
	}
	.back {
		position: fixed;
		width: 50rpx;
		height: 50rpx;
		left: 30rpx;
		top: 30rpx;
		z-index: 100;
	}
	.video-img {
		width: 100%;
		height: 225px;
		background-size: 100% 100%;
		text-align: center;
	}
	.content {
		width: 100%;
		padding: 0 0 0 0;
		background-color: #fff;
		box-shadow: 0upx 0upx 25upx rgba(0,0,0,0.1);
		border-radius: 15upx;
		margin-top: 20rpx;
	}
</style>
